<?php
/**
 * 活动投放的卡券
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019-03-28
 * Time: 下午 4:39
 */
namespace Model;
use MongoDB\BSON\Timestamp;

class ActivityCouponMod extends Mod
{
	public function __construct()
	{
		$this->_table = 'kuyuan_vbz_activity_coupon';
	}

    /**
     * 门店端store获取投放列表
     * @param $_store_id
     * @param $_activity_id
     * @param $_type
     * @param array $_limit
     */
    public function getList($_store_id,$_activity_id,$_type,Array $_limit)
    {
        global $_W;
        $_where = " a.store_id = :store_id AND a.activity_id = :activity_id ";
        $_bind = array('store_id'=>$_store_id,'activity_id'=>$_activity_id);
        if($_type != 'qb'){
            $_bind['type'] = $_type;
            $_where .= "AND b.type = :type ";
        }
        $_sql = "SELECT 
                        a.id,a.status,a.goal,a.coupon_id,a.number,a.give_number,a.use_limit_type,a.limit_time,b.type,b.name,b.cost,b.rule
                    FROM
                        ".tablename($this->_table)." as a
                    LEFT JOIN
                        ".tablename('kuyuan_vbz_store_coupon')." as b
                    on
                        a.coupon_id = b.id
                    WHERE
                        ".$_where."
                    GROUP BY
                        a.coupon_id
                    ORDER BY
                        a.coupon_id DESC
                    LIMIT
                        {$_limit[0]},{$_limit[1]}
                 ";
       return pdo_fetchall($_sql,$_bind);
    }

    /**
     * 活动后台管理端admin获取投放列表
     * @param $_store_id
     * @param $_activity_id
     * @param $_status
     * @param array $_limit
     * @return
     */
    public function adminGetList($_activity_id,$_status,Array $_limit)
    {
        global $_W;
        $_where = " a.activity_id = :activity_id AND a.status = :status";
        $_bind = array('activity_id'=>$_activity_id,'status'=>$_status);
        $_sql = "SELECT 
                        a.id,a.goal,a.number,a.give_number,a.use_limit_type,a.limit_time,b.type,b.name,b.cost,b.rule,c.name AS store_name,c.address
                    FROM
                        ".tablename($this->_table)." as a
                    LEFT JOIN
                        ".tablename('kuyuan_vbz_store_coupon')." as b
                    on
                        a.coupon_id = b.id
                    RIGHT JOIN 
                        ".tablename('kuyuan_vbz_store')." as c
                    on
                        b.store_id = c.id
                    WHERE
                        ".$_where."
                    ORDER BY
                        a.coupon_id DESC
                    LIMIT
                        {$_limit[0]},{$_limit[1]}
                 ";
        return pdo_fetchall($_sql,$_bind);
    }

    /**
     * 获取门店卡券的数据总数
     * @param $_activity_id 活动id
     * @return 返回数量
     */
    public function putCouponDataTotal($_activity_id)
    {
        $_sql = "SELECT 
						count('id') 
					FROM
						".tablename($this->_table)."
					WHERE
			            activity_id = :activity_id
			        AND
			            status = :status
			        AND
			            goal = :goal	
			        AND
			            give_number < number 
				";
        return pdo_fetchcolumn($_sql,array(
            'activity_id'=>$_activity_id,
            'status'=>1,
            'goal'=>1
        ));
    }

    /**
     * App端获取开宝箱时或者投放卡券的列表
     * 列表条件 1、卡券门店状态正常
     *2、卡券未过期
     *3、卡券有库存
     *4、当前用户是否已经抢到足够的卡券
     * @param $_activity_id 活动id
     * @param $_limit 返回下标数量
     * @return sql result
     */
    public function putCoupon($_activity_id,$_limit)
    {
        global $_W;
        $_where = "
                        A.activity_id = :activity_id
                    AND
                        A.status = :coupon_status
                    AND
                        C.uniacid = :uniacid
                   AND
                         B.uniacid = :uniacid
                   AND
                        B.status = :coupon_status
                   AND
                         A.goal = :goal
                   AND
                        A.give_number < A.number
                   AND
                        C.status = :store_status
                  ";
        $_bind = array('uniacid'=>$_W['account']['uniacid'],'goal'=>1,'activity_id'=>$_activity_id,'store_status'=>1,'coupon_status'=>1,'coupon_status'=>1);
        $_sql = "
                 SELECT 
                        A.id,A.store_id,A.user_number,A.use_limit_type,A.limit_time,A.coupon_id,B.name,B.images,B.edit_time,B.rule,B.type,B.cost,C.name as store_name,C.logo
                 FROM 
                        ".tablename($this->_table)." as A
                 LEFT JOIN
                        ".tablename('kuyuan_vbz_store_coupon')." as B
                 ON
                        A.coupon_id = B.id
                 RIGHT JOIN
                        ".tablename('kuyuan_vbz_store')." as C
                 ON
                        A.store_id = C.id
                 WHERE
                       ".$_where."
                 LIMIT
                    ".$_limit[0].",".$_limit[1]."
                ";
        return pdo_fetchall($_sql,$_bind);
    }

    /**
     * 获取指定门店投放卡券中的总数
     */
    public function putCouponTotal($_store_id)
    {
        $_sql = "SELECT 
						SUM(A.`number`)-SUM(A.`give_number`)
					FROM
						".tablename($this->_table)." as A
					LEFT JOIN
					    ".tablename('kuyuan_vbz_activity')." B
					ON
					    A.activity_id = B.id
					WHERE
					    A.store_id = :store_id
					AND
					    A.number > A.give_number
				    AND
				        A.status = 1
				    AND
				        B.status = 1
				    AND
				        B.start < :start_time
				    AND
				        B.end > :end_time
				";
        $_sum = pdo_fetchcolumn($_sql,array('store_id'=>$_store_id,'start_time'=>TIMESTAMP,'end_time'=>TIMESTAMP));
        return $_sum ? $_sum : 0;
    }
}